package com.atguigu.eduorder.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.eduorder.entity.Order;
import com.atguigu.eduorder.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author faiz
 * @since 2021-05-19
 */
@Api("订单管理")
@RestController
@RequestMapping("/eduorder/order/")
//@CrossOrigin
public class OrderController {
    @Autowired
    private OrderService orderService;

    //生成订单的方法
    @ApiOperation(value = "生成订单的方法")
    @PostMapping("createOrder/{courseId}")
    public R createOrder(@PathVariable("courseId") String courseId,
                         HttpServletRequest request) {
        String uId = JwtUtils.getMemberIdByJwtToken(request);
        String orderNo = orderService.createOrder(courseId, uId);
        return R.ok().data("orderId", orderNo);
    }

    @ApiOperation(value = "根据订单id查询订单")
    @GetMapping("getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable("orderId") String orderId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderId);
        Order order = orderService.getOne(wrapper);
        return R.ok().data("item", order);
    }

    @ApiOperation("根据用户id,课程id查询是否付款")
    @GetMapping("isBuyCourse/{courseId}/{memberId}")
    public boolean isBuyCourse(@PathVariable("courseId") String courseId,
                               @PathVariable("memberId") String memberId) {
        System.out.println("buybuybuybuybuybuybuy");
        System.out.println("courseId:" + courseId);
        System.out.println("memberId:" + memberId);
        System.out.println("buybuybuybuybuybuybuy");
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id", memberId);
        wrapper.eq("course_id", courseId);
        wrapper.eq("status", 1); //支付状态
        int count = orderService.count(wrapper);
        return count > 0;
    }
}

